# 测试Redis连接的PowerShell脚本
Write-Host "Testing Redis connection..."

# 测试端口连接
$connection = Test-NetConnection -ComputerName 127.0.0.1 -Port 6379 -InformationLevel Quiet
if ($connection) {
    Write-Host "✓ Redis port 6379 is accessible" -ForegroundColor Green
} else {
    Write-Host "✗ Cannot connect to Redis port 6379" -ForegroundColor Red
    exit 1
}

# 检查Redis进程
$redisProcess = Get-Process | Where-Object {$_.ProcessName -like "*redis*"}
if ($redisProcess) {
    Write-Host "✓ Redis process is running:" -ForegroundColor Green
    $redisProcess | Select-Object ProcessName, Id, CPU | Format-Table
} else {
    Write-Host "✗ No Redis process found" -ForegroundColor Red
}

# 尝试使用telnet测试Redis连接（如果可用）
try {
    $socket = New-Object System.Net.Sockets.TcpClient
    $socket.Connect("127.0.0.1", 6379)
    if ($socket.Connected) {
        Write-Host "✓ Successfully connected to Redis via TCP" -ForegroundColor Green
        
        # 尝试发送PING命令
        $stream = $socket.GetStream()
        $command = [System.Text.Encoding]::ASCII.GetBytes("PING`r`n")
        $stream.Write($command, 0, $command.Length)
        
        # 读取响应
        $buffer = New-Object byte[] 1024
        $response = $stream.Read($buffer, 0, 1024)
        $responseText = [System.Text.Encoding]::ASCII.GetString($buffer, 0, $response)
        
        Write-Host "Redis response to PING: $responseText" -ForegroundColor Yellow
        
        $socket.Close()
    }
} catch {
    Write-Host "✗ Failed to connect via TCP: $($_.Exception.Message)" -ForegroundColor Red
}

Write-Host "`nRedis connection test completed." 